<template>
  <div class="customer-jifen-container">
    <vab-query-form>
      <vab-query-form-left-panel style="padding-top: 6px">
        <el-descriptions title="客户积分管理" :column="3" border />
      </vab-query-form-left-panel>
      <vab-query-form-right-panel>
        <el-form :inline="true" :model="queryForm" @submit.prevent>
          <el-space>
            <el-form-item>
              <el-select v-model="queryForm.city" style="width: 130px">
                <el-option :value="0" label="请选择区域" />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-input
                v-model.trim="queryForm.title"
                clearable
                placeholder="可输入想要查询的关键字"
              />
            </el-form-item>
            <el-form-item>
              <el-button :icon="Search" type="primary" @click="queryData">
                查询
              </el-button>
            </el-form-item>
          </el-space>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-tabs
      v-model="queryForm.tab"
      type="border-card"
      @tab-click="handleTabClick"
    >
      <el-tab-pane label="合作中客户" name="serviceing">
        <el-table
          v-loading="listLoading"
          :data="list"
          @selection-change="setSelectRows"
        >
          <el-table-column
            align="center"
            show-overflow-tooltip
            type="selection"
          />
          <el-table-column
            align="center"
            label="客户ID"
            prop="CustomerID"
            show-overflow-tooltip
            width="80"
          />
          <el-table-column
            align="center"
            label="客户名称"
            prop="CustomerAllName"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="地址"
            prop="Address"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="电话"
            prop="Telphone"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="业务联系人"
            prop="BusinessPerson"
            show-overflow-tooltip
          />
          <!-- <el-table-column align="center" label="详细信息" width="100">
            <template #default="{ row }">
              <el-button text @click="goCustomer(row)">详情</el-button>
            </template>
          </el-table-column>
          <el-table-column align="center" label="业务单" width="100">
            <template #default="{ row }">
              <el-button text @click="goOrder(row)">业务单</el-button>
            </template>
          </el-table-column> -->
          <el-table-column align="center" label="积分余额" width="100">
            <template #default="{ row }">
              <div
                v-if="row.Jifen > 0"
                style="color: green; cursor: pointer"
                @click="toPointsList(row)"
              >
                {{ row.Jifen }}
              </div>
              <div
                v-else-if="row.Jifen < 0"
                style="color: red; cursor: pointer"
                @click="toPointsList(row)"
              >
                {{ row.Jifen }}
              </div>
              <div v-else style="cursor: pointer" @click="toPointsList(row)">
                {{ row.Jifen }}
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" label="积分详情" width="200">
            <template #default="{ row }">
              <el-button type="primary" @click="toPointsList(row)">
                积分详情
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          :current-page="queryForm.pageNo"
          :layout="layout"
          :page-size="queryForm.pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </el-tab-pane>
      <el-tab-pane label="已终止合作客户" name="terminated">
        <el-table
          v-loading="listLoading"
          :data="list"
          @selection-change="setSelectRows"
        >
          <el-table-column
            align="center"
            show-overflow-tooltip
            type="selection"
          />
          <el-table-column
            align="center"
            label="客户ID"
            prop="CustomerID"
            show-overflow-tooltip
            width="80"
          />
          <el-table-column
            align="center"
            label="客户名称"
            prop="CustomerAllName"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="地址"
            prop="Address"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="电话"
            prop="Telphone"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="业务联系人"
            prop="BusinessPerson"
            show-overflow-tooltip
          />
          <!-- <el-table-column align="center" label="历史跟进" width="200">
            <template #default="{ row }">
              <el-button text @click="getCustomerTrack(row)">
                历史跟进
              </el-button>
            </template>
          </el-table-column>
          <el-table-column align="center" label="详细信息" width="100">
            <template #default="{ row }">
              <el-button text @click="goCustomer(row)">详情</el-button>
            </template>
          </el-table-column>
          <el-table-column align="center" label="业务单" width="100">
            <template #default="{ row }">
              <el-button text @click="goOrder(row)">业务单</el-button>
            </template>
          </el-table-column> -->
          <el-table-column align="center" label="积分余额" width="100">
            <template #default="{ row }">
              <div
                v-if="row.Jifen > 0"
                style="color: green; cursor: pointer"
                @click="toPointsList(row)"
              >
                {{ row.Jifen }}
              </div>
              <div
                v-else-if="row.Jifen < 0"
                style="color: red; cursor: pointer"
                @click="toPointsList(row)"
              >
                {{ row.Jifen }}
              </div>
              <div v-else style="cursor: pointer" @click="toPointsList(row)">
                {{ row.Jifen }}
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" label="积分详情" width="200">
            <template #default="{ row }">
              <el-button type="primary" @click="toPointsList(row)">
                积分详情
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          :current-page="queryForm.pageNo"
          :layout="layout"
          :page-size="queryForm.pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </el-tab-pane>
    </el-tabs>
    <edit ref="editRef" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import Edit from './components/CustomerJifenEdit'
  import { getList } from '@/api/customerSigned'
  import { Delete, Plus, Search } from '@element-plus/icons-vue'

  export default defineComponent({
    name: 'CustomerJifen',
    components: {
      Edit,
    },
    setup() {
      const router = useRouter()
      const state = reactive({
        editRef: null,
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 1,
        selectRows: '',
        queryForm: {
          tab: 'serviceing',
          pageNo: 1,
          pageSize: 10,
          title: '',
        },
      })

      const handleTabClick = (tab) => {
        console.log(tab.props.name)
        state.queryForm.tab = tab.props.name
        fetchData()
      }

      const businessTrack = (row) => {
        router.push({
          path: '/customer/track',
          query: { customerid: row.CustomerID },
        })
      }
      const getCustomerTrack = (row) => {
        router.push({
          path: '/customer/trackingBusiness',
          query: { customerid: row.CustomerID },
        })
      }
      const setSelectRows = (val) => {
        state.selectRows = val
      }
      const handleEdit = (row) => {
        if (row.id) {
          state.editRef.showEdit(row)
        } else {
          state.editRef.showEdit()
        }
      }
      const goCustomer = (row) => {
        router.push({
          path: '/customer/info',
          query: { customerid: row.CustomerID },
        })
      }

      const goOrder = (row) => {
        router.push({
          path: '/business/order/orderList',
          query: { customerid: row.CustomerID },
        })
      }

      const handleSizeChange = (val) => {
        state.queryForm.pageSize = val
        fetchData()
      }
      const handleCurrentChange = (val) => {
        state.queryForm.pageNo = val
        fetchData()
      }
      const queryData = () => {
        state.queryForm.pageNo = 1
        fetchData()
      }
      const fetchData = async () => {
        state.listLoading = true
        const {
          data: { list, total },
        } = await getList(state.queryForm)
        state.list = list
        state.total = total
        state.listLoading = false
      }

      const toPointsList = (row) => {
        console.log(row, '积分列表')
        if (row.CustomerID) {
          state.editRef.showEdit(row)
        } else {
          state.editRef.showEdit()
        }
      }
      onMounted(() => {
        fetchData()
      })

      return {
        ...toRefs(state),
        businessTrack,
        setSelectRows,
        toPointsList,
        getCustomerTrack,
        handleEdit,
        goCustomer,
        goOrder,
        handleSizeChange,
        handleCurrentChange,
        queryData,
        handleTabClick,
        fetchData,
        Delete,
        Plus,
        Search,
      }
    },
  })
</script>

<style>
  .fa {
    cursor: pointer;
  }
</style>
